//
//  CollectViewController.m
//  Journey
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CollectViewController.h"
#import "HotModel.h"
#import "HotDetailViewController.h"
#import "CollectDViewController.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation CollectViewController

-(void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];
    self.array = [NSMutableArray arrayWithArray:[[DataBaseHandle shareDataBaseHandle] selectHot:userName withCompare:YES]];
  
    NSLog(@"%@",self.array);
   
    [self.tableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.navigationItem.title = @"我的收藏";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  HotDetailViewController *hotDetail = [[HotDetailViewController alloc] init];
    
    HotModel *model = [self.array objectAtIndex:indexPath.row];
    
//    hotDetail.type = @"111";
    
 ///   hotDetail.HotString = model.appview_url;
    CollectDViewController *collect = [[CollectDViewController alloc] init];
    collect.type = @"111";
    collect.string = model.appview_url;
    
    
    [self.navigationController pushViewController:collect animated:YES];
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    HotModel *model = [self.array objectAtIndex:indexPath.row];
    [[DataBaseHandle shareDataBaseHandle]hotDeleteTable:model.title];
    [self.array removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    HotModel *model = self.array[indexPath.row];
    
    cell.textLabel.text = model.title;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)leftButtonAction{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
